package com.gmzg.gmzgnews.me.view;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.me.model.User;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.login_toolbar)
    Toolbar mLoginToolbar;
    @BindView(R.id.quit_login_btn)
    Button mQuitLoginBtn;
    @BindView(R.id.setting_username)
    TextView mSettingUsername;
    @BindView(R.id.image)
    CircleImageView mImage;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setSupportActionBar(mLoginToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        sp = getSharedPreferences(MeFragment.SP_USER, MODE_PRIVATE);
        String username = sp.getString(MeFragment.USERNAME, null);
        String image = sp.getString(MeFragment.USER_IMAGE, null);
        if (username != null) {
            mSettingUsername.setText(username);
        }

        if (image != null) {
            if (image.startsWith("http:")) {
                Picasso.with(this)
                        .load(image)
                        .resize(200, 200)
                        .into(mImage);
            } else {
                Uri uri = Uri.fromFile(new File(image));
                Picasso.with(this)
                        .load(uri)
                        .resize(200, 200)
                        .into(mImage);
            }

        }
    }

    @OnClick(R.id.quit_login_btn)
    public void onClick() {
        new AlertDialog.Builder(this)
                .setCancelable(true)
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.clear();
                        editor.apply();
                        EventBus.getDefault().post("退出");
                        finish();
                    }
                })
                .setNegativeButton("再看看", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}
